//wontube blog
$(function(){
	blog_update.run();
});

var blog_update = {
		run: function(){
			this._init_();
			this._start_();
			this._init_events();			
		},
		
		_init_: function(){			
			this.contextPath = $("#contextPath").val();
			this.blogId = $("#blogId").val();
		},
		
		_start_: function(){			
			CKEDITOR.replace( 'blog_content', {
				height: '456px'
			});
		},
		
		_init_events: function(){
			$("#add_blog_btn").click(function(){
				blog_update._handle_save_blog();
			});
			
			$("#update_blog_btn").click(function(){
				blog_update._handle_save_blog('update');
			});
		},
		
		_handle_save_blog: function(type){
			var data = $("#add_blog_form").serialize();
			var content = CKEDITOR.instances.blog_content.getData();
			data += "&content=" + encodeURIComponent(content);
			var url = blog_update.contextPath + "/admin/blog/add";
			if(type == 'update'){
				url = blog_update.contextPath + "/admin/blog/update";
			}
			$.post(url, data, function(result){
				if(result.indexOf('Done') == 0){
					var id = blog_update.blogId || result.substring(4);
					location.href = blog_update.contextPath + "/admin/blog/preview/" + id;				
				}else{
					alert(result);
				}
			});
		}
};